package com.sh.caryr.erp.controller.basis;

import com.sh.caryr.erp.entity.ResultMessage;
import com.sh.caryr.erp.entity.basis.CompCfg;
import com.sh.caryr.erp.entity.basis.ExpressComp;
import com.sh.caryr.erp.entity.user.User;
import com.sh.caryr.erp.service.ICompCfgService;
import com.sh.caryr.erp.util.UtilityTool;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/util")
@SessionAttributes("user")
public class UtilController {

    @Autowired
    private ICompCfgService compCfgService;

    @ApiOperation(value = "获取快递公司列表信息", notes = "获取快递公司列表信息")
    @RequestMapping(value = "/expressComp", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<ExpressComp>> findExpressComp() {
        List<ExpressComp> expressComps = UtilityTool.getExpressComps();
        return new ResultMessage<>(expressComps);
    }


    @ApiOperation(value = "Company code configuration")
    @RequestMapping(value = "/compCfg", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<CompCfg>> findExpressComp(@ModelAttribute("user") User operator) {
        List<CompCfg> compCfgs = compCfgService.loadCompCfgByCoco(operator.getCoco());
        if(CollectionUtils.isNotEmpty(compCfgs)){
            for(CompCfg cfg:compCfgs){
                cfg.setAccessToken("***");
                cfg.setAppKey("***");
                cfg.setAppSecret("***");
            }
        }
        return new ResultMessage<>(compCfgs);
    }
}
